// pages/submitInfo/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    titles:"",
    contents:"",
    tempFilelist:[],
    uploadImgList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  //添加留言记录
  insertInfo:function(){
    var that = this
    var uploadImgList = that.data.uploadImgList
    uploadImgList = uploadImgList.join()
    wx.request({
      url: app.globalData.host +  "/api/Message/Insert?storeId=" + app.globalData.storeId + "&title=" + that.data.titles + "&content=" + that.data.contents + "&imgs=" + uploadImgList,
      success: function(res) {
        wx.hideLoading()
        if(res.data.Code == 0){
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              wx.navigateBack({
                delta: 1,
              })
            }
          })

        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            cancel: false
          })
          return
        }
      }
    })
  },

  uploadImg: function () {//上传头像
    var that = this
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        that.setData({
          tempFilelist: tempFilePaths
        })
      }
    })
  },

  subumitInfo: function () {//这里触发图片上传的方法
    wx.showLoading({
      title: '提交中',
    })
    var that = this;
    var pics = that.data.tempFilelist;
    if (pics.length>0){
      that.uploadimg({
        url: app.globalData.host + '/api/tool/upload?operation=uploaduserreview',//这里是你图片上传的接口
        path: pics//这里是选取的图片的地址数组
      });
    }else{
      that.insertInfo()
    }
    
  },
  //多张图片上传
  uploadimg: function (data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'fileData',
      formData: null,
      success: (resp) => {
        success++;
        var newArr = []
        var str = resp.data
        var pic = JSON.parse(str);
        var picImg = pic.Data
        newArr.push(picImg)
        if (that.data.uploadImgList.length > 0) {
          var ImgList = that.data.uploadImgList
          newArr = ImgList.concat(newArr)
        }
        that.setData({
          uploadImgList: newArr
        })
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++;
        if (i == data.path.length) {  //当图片传完时，停止调用     
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
          that.insertInfo()
        } else {//若图片还没有传完，则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }

      }
    });
  },

  getTitle:function(e){
    this.setData({
      titles:e.detail.value
    })
  },
  getContent:function(e){
    this.setData({
      contents: e.detail.value
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})